package cn.tedu.product.controller;

import cn.tedu.product.service.ProductService;

import com.jt.common.pojo.Product;
import com.jt.common.pojo.User;
import com.jt.common.vo.EasyUIResult;

import com.jt.common.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
@RequestMapping("/product/manage")
public class ProductController {
    @Autowired
    private ProductService productService;
    //分页查询商品的list
    @RequestMapping("/pageManage")
    public EasyUIResult queryByPage(Integer page,Integer rows){
        return productService.queryByPage(page,rows);
    }
    @RequestMapping("item/{productId}")
    public Product queryOneProduct(@PathVariable(name = "productId") String productId,String userId){
        if(userId==""||userId==null){
            return productService.queryOneProduct(productId);
        }
        return productService.queryOneProduct(productId,userId);
    }

    @RequestMapping("/save")
    public SysResult saveProduct(Product product){
        try {
            productService.addProduct(product);
            return SysResult.ok();
        }catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(201,"新增失败",null);
        }
    }
    @RequestMapping("/update")
    public SysResult editProduct(Product product){
        try {
            productService.editProduct(product);
            return SysResult.ok();
        }catch (Exception e) {
            e.printStackTrace();
            return SysResult.build(201,"更新失败",null);
        }
    }
    //今日推荐
    @RequestMapping("/recommend")
    public EasyUIResult recommendProducts(){
        return productService.recommendProducts();
    }
    //猜你喜欢
    @RequestMapping("/guess")
    public EasyUIResult guessProducts(String userId){
        //todo 传入真实user
        return productService.guessProducts(userId);
    }
}
